import Vue from 'vue'
import Router from 'vue-router'
import AdminRouter from './admin'
import Welcome from '../Pages/Home/Welcome'
import Login from '../Pages/login/Login'
import home from '../Pages/Home/home'
import addRiChang from '../Pages/Home/addRiChang'
import Find from '../Pages/Find/index'
import Shop from '../Pages/Shop/shop'
import CosBa from '../Pages/Other/CosBa'
import Reg from '../Pages/login/Reg'
import My from '../Pages/My/my'
import err404 from '../Pages/err404'
import Setting from '../Pages/My/Setting'
import DongTaiDetail from '../Pages/Other/DongTaiDetail'
import DianZanList from '../Pages/Other/DianZanList'
import AddVedio from '../Pages/Other/AddVedio'
import AddDongTai from '../components/AddDongTai'
import shopDetail from '../Pages/Shop/shopDetail'
import createOrder from '../Pages/Shop/createOrder'
import AddAddress from '../Pages/Shop/AddAddress'
import Pay from '../Pages/Shop/Pay'
import UserDetail from '../Pages/Other/UserDetail'
import GuanZhuList from '../Pages/My/GuanZhuList'
import FansList from '../Pages/My/FansList'
import AddPhotograph from '../Pages/Other/AddPhotograph'
import AddArticle from '../Pages/Other/AddArticle'
import Sign from '../Pages/My/Sign'
import EditInfo from '../Pages/Other/EditInfo'
Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/',
      name: '/',
      component: Welcome,
      redirect: '/welcome'
    },
    {
      path: '/welcome',
      name: 'welcome',
      component: Welcome
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/reg',
      name: 'reg',
      component: Reg
    },
    // 主页面路由
    {
      path: '/home',
      name: 'home',
      component: home,
      meta: {
        keepAlive: false // 使用缓存
      }
    },
    {
      path: '/dongtaiDetail',
      name: 'dongtaiDetail',
      component: DongTaiDetail
    },
    {
      path: '/dianzanlist',
      name: 'dianzanlist',
      component: DianZanList
    },
    {
      path: '/userDetail',
      name: 'userDetail',
      component: UserDetail
    },
    {
      path: '/addDongTai',
      name: 'addDongTai',
      component: AddDongTai
    },
    {
      path: '/addrichang',
      name: 'addrichang',
      component: addRiChang
    },
    {
      path: '/addPhotograph',
      name: 'addPhotograph',
      component: AddPhotograph
    },
    {
      path: '/addvedio',
      name: 'addvedio',
      component: AddVedio
    },
    {
      path: '/addArticle',
      name: 'addArticle',
      component: AddArticle
    },
    // 发现界面的路由定义
    {
      path: '/find',
      name: 'find',
      component: Find,
      meta: {
        keepAlive: true // 使用缓存
      }
    },
    // 商城界面定义
    {
      path: '/shop',
      name: 'shop',
      component: Shop
    },
    {
      path: '/cosba',
      name: 'cosba',
      component: CosBa
    },
    {
      path: '/shopDetail',
      name: 'shopDetail',
      component: shopDetail
    },
    {
      path: '/createorder',
      name: 'createorder',
      component: createOrder
    },
    {
      path: '/addaddress',
      name: 'addaddress',
      component: AddAddress
    },
    {
      path: '/pay',
      name: 'pay',
      component: Pay
    },
    // 我的页面路由定义位置
    {
      path: '/my',
      name: 'my',
      component: My
    },
    {
      path: '/setting',
      name: 'setting',
      component: Setting
    },
    {
      path: '/guanZhuList',
      name: 'guanZhuList',
      component: GuanZhuList
    },
    {
      path: '/fansList',
      name: 'fansList',
      component: FansList
    },
    {
      path: '/sign',
      name: 'sign',
      component: Sign
    },
    {
      path: '/editInfo',
      name: 'editInfo',
      component: EditInfo
    },
    {
      path: '/404',
      name: '404',
      component: err404
    }
  ].concat(AdminRouter)
})
export default router
// 全局路由守卫
// router.beforeEach((to, from, next) => {
//   const nextRoute = ['home', 'addrichang', 'find', 'shop', 'cosba'] // 需要登录的页面
//   console.log(to.name)
//   let isLogin = localStorage.getItem('userId') // 判断是否登录，本地存储有用户数据则视为已经登录
//   // 未登录状态；当路由到 nextRoute 指定页时，跳转至 UserLogIn
//   if (nextRoute.includes(to.name)) { // 检测是否需要登录的页面
//     if (!isLogin) { // 如果未登录（本地存储无用户数据），并且要跳到登录页面
//       next('/login')
//       return
//     }
//     // 登录后，跳到到当前页面
//     router.push('/home')
//   }
//   // 已登录状态；当路由到 UserLogIn 时，跳转至 Home
//   if (to.name === '/login') {
//     if (isLogin) {
//       next('/home')
//       return
//     }
//   }
//   next() // 必须使用 next ,执行效果依赖 next 方法的调用参数
// })
router.beforeEach((to, from, next) => {
  let isLogin = localStorage.getItem('userId')
  if (to.matched.length === 0) { // 没有匹配到当前路由
    next('/404')
  } else if (!isLogin && to.path !== '/login' && to.path !== '/reg') {
    if (from.path === '/welcome') {
      next('/welcome')
    } else {
      next()
    }
  } else {
    if ((to.path === '/login' || to.path === '/reg') && isLogin) {
      // 如果已经登录，却尝试访问登录页面或者错误页面，将继续保持原本的页面
      next(from.path)
    } else {
      next()
    }
  }
})
